package com.iwhalecloud.bss.kite.cucc.service.attr.handler;

import com.iwhalecloud.bss.kite.common.constant.KeyConsts;
import com.iwhalecloud.bss.kite.common.constant.ServiceOfferConsts;
import com.iwhalecloud.bss.kite.common.util.KiteObjectUtils;
import com.iwhalecloud.bss.kite.common.util.KiteStringUtils;
import com.iwhalecloud.bss.kite.cucc.common.constant.LocalKeyConsts;
import com.iwhalecloud.bss.kite.cucc.service.util.ProdCateUtil;
import com.iwhalecloud.bss.kite.manager.api.IOperateSceneInstService;
import com.iwhalecloud.bss.kite.manager.api.IQuerySceneInstService;
import com.iwhalecloud.bss.kite.manager.inst.KiteAttr;
import com.iwhalecloud.bss.kite.manager.inst.KiteInst;
import com.iwhalecloud.bss.kite.manager.inst.KiteProdInst;
import com.iwhalecloud.bss.kite.service.attr.AttrData;
import com.iwhalecloud.bss.kite.service.attr.data.CustAttrData;
import com.iwhalecloud.bss.kite.service.attr.handler.AbstractAttrHandler;
import com.iwhalecloud.bss.kite.service.scene.attr.OperateInstAttrService;
import com.ztesoft.bss.common.exception.BssException;
import com.ztesoft.bss.common.util.SpringUtil;
import org.springframework.beans.factory.annotation.Autowired;

import java.util.List;

/**
 * @author liaojing
 * NB物联网水务抄表服务-超声波--订购数量属性联动
 * @create 2022-01-19-09:34
 */

public class WaterMeterReadingServiceOrderQuantityAttrHandler extends AbstractAttrHandler<KiteProdInst, AttrData> {
    @Autowired
    private IOperateSceneInstService operateSceneInstService;

    @Autowired
    IQuerySceneInstService querySceneInstService;

    @Autowired
    OperateInstAttrService operateInstAttrService;

    @Override
    public void before(KiteAttr attr, KiteProdInst inst, String value, AttrData data) {
        super.before(attr, inst, value, data);
        String sceneInstId = data.getSceneInstId();
        //模版还原时，不做校验
        if ("sceneTempl".equals(data.getExtAttributes().get("entry"))) {
            return;
        }
        //变更业务的场景初始化过程中不校验
        IQuerySceneInstService querySceneInstService = SpringUtil.getBean(IQuerySceneInstService.class);
        if (!ServiceOfferConsts.SERVICE_OFFER_ORDER.equals(data.getServiceOfferId())
                && !KeyConsts.IFTRUE_T.equals(querySceneInstService.getExtParameterValue(data.getSceneInstId(), LocalKeyConsts.BUSI_CHANGE_SCENE))) {
            return;
        }
        List<KiteProdInst> kiteProdInstList = querySceneInstService.listProdInsts(sceneInstId);
        // 获取子产品
        KiteProdInst subProdInst = kiteProdInstList.stream().filter(kiteProdInst -> KiteStringUtils.isEqual(kiteProdInst.getSpecId(), "50000029")).findFirst().get();
        // 没有获取到子产品，则退出
        int contractNum = 0;
        if (KiteObjectUtils.isEmpty(subProdInst)) {
            return;
        }else {
            String num = subProdInst.getAttr("200003677").getValue();
            if(!KiteStringUtils.isEqual(num,null) && !KiteStringUtils.isEqual(value,null)){
                contractNum = Integer.parseInt(num);
            }
        }
        if(contractNum == 0){
            return;
        }else{
          int orderNum = Integer.parseInt(value);
            if (orderNum > contractNum) {
                BssException.throwOut("'订购数量'不允许大于'合同约定订购数量'", "'订购数量'不允许大于'合同约定订购数量'", "'订购数量'不允许大于'合同约定订购数量'");
            }
        }
    }

    @Override
    public void after(KiteAttr attr, KiteProdInst inst, AttrData data) {
        super.after(attr, inst, data);
        //产品变更 延期分组置灰
        if (KiteStringUtils.equalsAny(data.getServiceOfferId(), "2826", "2024")
                && (KiteStringUtils.isEqual(attr.getGroupId(), "300000173") || KiteStringUtils.equalsAny(inst.getSpecId(), ProdCateUtil.getMainMemberProdId(ProdCateUtil.GIFT_NBSWCB_PROD)))) {
            attr.setIsEdit(KeyConsts.IFTRUE_F);
            operateSceneInstService
                    .setAttr(data.getSceneInstId(), inst.getInstType(), inst.getInstId(), attr);
        }

    }
}
